package com.example.node.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.example.node.R;
import com.example.node.adapter.LifeStyleAdapter;
import com.example.node.bean.LifeStyleBean;
import com.example.node.entity.Constant;
import com.example.node.impl.WeatherApi;
import com.example.node.model.LifeStyleModel;
import com.example.node.model.NowWeatherModel;
import com.example.node.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MeFragment extends Fragment {

    private Retrofit retrofit;
    private WeatherApi api;
    private TextView tv_city;
    private TextView tv_cond_text;
    private TextView tv_wind_dir;
    private TextView tv_tmp;
    private RecyclerView mLifeStyleView;
    private List<LifeStyleBean> mList = new ArrayList<>();
    private LifeStyleAdapter lifeStyleAdapter;
    private ScrollView mScrollView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        initWeatherApi();
        tv_city = view.findViewById(R.id.tv_city);
        tv_cond_text = view.findViewById(R.id.tv_cond_text);
        tv_wind_dir = view.findViewById(R.id.tv_wind_dir);
        tv_tmp = view.findViewById(R.id.tv_tmp);
        mLifeStyleView = view.findViewById(R.id.mLifeStyleView);
        lifeStyleAdapter = new LifeStyleAdapter(mList);
        //ScrollView 惯性滑动失效 禁止 RecyleView滑动
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity()){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        mLifeStyleView.setLayoutManager(linearLayoutManager);
        mLifeStyleView.setAdapter(lifeStyleAdapter);
        mScrollView = view.findViewById(R.id.mScrollView);
    }

    private void initWeatherApi() {
        retrofit = new Retrofit.Builder()
                .baseUrl(Constant.WEATHER_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //获取实时天气
        api = retrofit.create(WeatherApi.class);
        api.getNowWeather(Constant.WEATHER_CITY, Constant.WEATHER_KEY)
                .enqueue(new Callback<NowWeatherModel>() {
                    @Override
                    public void onResponse(Call<NowWeatherModel> call, Response<NowWeatherModel> response) {
                        if (response.isSuccessful()) {
                            NowWeatherModel model = response.body();
                            LogUtils.I(model.getHeWeather6().get(0).getBasic().getLocation());
                            parsingNowWeather(model);
                        }
                    }

                    @Override
                    public void onFailure(Call<NowWeatherModel> call, Throwable t) {
                    }
                });
        //获取生活指数
        api.getLifeStyle(Constant.WEATHER_CITY, Constant.WEATHER_KEY).enqueue(new Callback<LifeStyleModel>() {
            @Override
            public void onResponse(Call<LifeStyleModel> call, Response<LifeStyleModel> response) {
                if (response.isSuccessful()) {
                    LifeStyleModel model = response.body();
                    parsingLiftStyle(model);
                }
            }

            @Override
            public void onFailure(Call<LifeStyleModel> call, Throwable t) {

            }
        });
    }

    private void parsingLiftStyle(LifeStyleModel model) {
        for (int i = 0; i < model.getHeWeather6().get(0).getLifestyle().size(); i++) {
            LifeStyleBean lifeStyleBean = new LifeStyleBean();
            lifeStyleBean.setBrf(model.getHeWeather6().get(0).getLifestyle().get(i).getBrf());
            lifeStyleBean.setTxt(model.getHeWeather6().get(0).getLifestyle().get(i).getTxt());
            mList.add(lifeStyleBean);
        }
        lifeStyleAdapter.notifyDataSetChanged();
    }

    //解析今天的天气
    private void parsingNowWeather(NowWeatherModel model) {
        tv_city.setText(model.getHeWeather6().get(0).getBasic().getCnty());
        tv_cond_text.setText(model.getHeWeather6().get(0).getNow().getCond_txt());
        tv_wind_dir.setText(model.getHeWeather6().get(0).getNow().getWind_dir());
        tv_tmp.setText(model.getHeWeather6().get(0).getNow().getTmp());
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            mScrollView.post(new Runnable() {
                @Override
                public void run() {
                    mScrollView.fullScroll(ScrollView.FOCUS_UP);
                }
            });
        }
    }
}
